// 防抖
export function debounce(func, duration = 1000) {
  let timerId;

  function _executor(...args) {
    clearTimeout(timerId);
    timerId = setTimeout(() => {
      func.call(this, ...args)
    }, duration);
  }
  // _executor.cancel=()=>{
  //   clearTimeout(timerId)
  // };
  return _executor
}

// 节流
export function throttle(func, duration = 1000) {
  let lastTime = 0;
  return function (...args) {
    let nowTime = new Date().getTime();
    if (nowTime - lastTime > duration) {
      func.call(this, ...args)
      lastTime = nowTime;
    }
  }
}

// 深拷贝
export function deepClone(obj) {
  let objClone = Array.isArray(obj) ? [] : {};
  if (obj && typeof obj === "object") {
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        // 判断ojb子元素是否为对象，如果是，递归复制
        if (obj[key] && typeof obj[key] === "object") {
          objClone[key] = deepClone(obj[key]);
        } else {
          // 如果不是，简单复制
          objClone[key] = obj[key];
        }
      }
    }
  }
  return objClone;
}


// 数组去重
export function uniqueArr(arr) {
  let result = [];
  let obj = {};
  for (let i = 0; i < arr.length; i++) {
    if (!obj[arr[i]]) {
      obj[arr[i]] = true;
      result.push(arr[i]);
    }
  }
  return result;
}

// 获取当前时间
export function dealWithTime(data) {
  // 获取当前时间
  let formatDateTime
  let Y = data.getFullYear()
  let M = data.getMonth() + 1
  let D = data.getDate()
  let H = data.getHours()
  let Min = data.getMinutes()
  let S = data.getSeconds()
  let W = data.getDay()
  H = H < 10 ? '0' + H : H
  Min = Min < 10 ? '0' + Min : Min
  S = S < 10 ? '0' + S : S
  formatDateTime = Y + '-' + M + '-' + D + ' ' + H + ':' + Min + ':' + S
  return formatDateTime
  //  this.nowWeek = "周" + W ;
  //  this.nowTime = H + ":" + Min + ":" + S;
  // formatDateTime = Y + "年" + M + "月" + D + "日 " + " 周" +W + H + ":" + Min + ":" + S;
}
// 获取当前日期为周几
export function getDayOfWeek(date) {
  var arr = new Array("日", "一", "二", "三", "四", "五", "六");
  var week = new Date(date).getDay();
  var str = "星期" + arr[week];
  return str;
}